<template>
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px">
      <el-form-item label="发票编号" prop="invoiceCode">
        <el-input v-model="queryParams.invoiceCode" placeholder="请输入发票编号"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="审核状态" prop="payStatus">
        <el-select clearable placeholder="请选择审核状态" v-model="queryParams.assessStatus">
          <el-option :key="dict.value"
                     :label="dict.label" :value="dict.value" v-for="dict in dict.type.apply_assess_status"/>
        </el-select>
      </el-form-item>

      <el-form-item label="回款状态" prop="payStatus">
        <el-select clearable placeholder="请选择回款状态" v-model="queryParams.payStatus">
          <el-option :key="dict.value"
                     :label="dict.label" :value="dict.value" v-for="dict in dict.type.apply_pay_status"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8" v-if="queryParams.assessStatus==='0'">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddHardware"
                   v-hasPermi="['develop:PayApply:add']">合并付款
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddDeposit"
                   v-hasPermi="['develop:PayApply:add']">申请定金
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddArrears"
                   v-hasPermi="['develop:PayApply:add']">申请尾款
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table border v-loading="loading"  :data="dataList" @selection-change="handleSelectionChange" ref="table"
              show-summary :summary-method="getSummaries"
              :max-height="tableMaxHeight"  @sort-change="handleSortChange">
      <el-table-column type="selection" width="80" align="center"/>
      <el-table-column label="序号" type="index" align="center" width="50"></el-table-column>
      <el-table-column sortable label="编号" align="center" prop="nos"/>
      <el-table-column sortable label="制单人" align="center" prop="createBy"/>
      <el-table-column sortable label="供应商名称 " align="center" prop="goodsSupplier.name" width="230"/>
      <el-table-column sortable align="center" label="付款类别" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pay_apply_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column sortable label="发票编号" align="center" prop="invoiceCode"/>
      <el-table-column  label="申请付款金额" align="center" prop="totalMoney"/>
      <el-table-column label="已付金额" align="center" prop="paidMoney"/>
      <el-table-column label="剩余金额" align="center" prop="remainMoney"/>
      <el-table-column align="center" label="审核状态" prop="assessStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.apply_assess_status" :value="scope.row.assessStatus"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="付款状态" prop="payStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.apply_pay_status" :value="scope.row.payStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-document" v-if="scope.row.assessStatus === '4'"
                     @click="payRecordList(scope.row)" v-hasPermi="['develop:LvBuy:edit']">打款记录
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-document"
                     @click="handleDetail(scope.row)" v-hasPermi="['develop:PayApply:edit']">详情
          </el-button>

          <el-button size="mini" type="text" icon="el-icon-printer"
                     @click="handlePrint(scope.row)" v-hasPermi="['develop:PayApply:edit']">打印
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList"/>
    <payApply-form ref="form" @ok="getList"></payApply-form>
    <pay-apply-detail ref="payApplyDetail"></pay-apply-detail>
    <pay-apply-detail-print ref="payApplyDetailPrint"></pay-apply-detail-print>
    <pay-record-list-module ref="payRecordList"></pay-record-list-module>

  </el-dialog>
</template>

<script>
import {accAdd, deleteAction, getAction} from '@/api/manage'
import PayApplyForm from '@/views/develop/payApply/module/PayApplyForm'
import LvBuyListModule from "@/views/develop/lvBuy/module/LvBuyListModule.vue";
import PayRecordForm from "@/views/develop/payRecord/module/PayRecordForm.vue";
import PayApplyDetail from "@/views/develop/payApply/module/PayApplyDetail.vue";
import PayRecordListModule from "@/views/develop/payRecord/module/PayRecordListModule.vue";
import PayApplyDetailPrint from "@/views/develop/payApply/module/PayApplyDetailPrint.vue";

export default {
  name: "PayApplyListModule",
  dicts: ["pay_apply_type", "apply_pay_status", "apply_assess_status"],
  components: {
    PayApplyDetailPrint,
    PayRecordListModule,
    PayApplyDetail,
    PayRecordForm,
    LvBuyListModule,
    PayApplyForm,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 付款申请单表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        buyId: null,
        invoiceCode: null,
        assessStatus: null,
        orderByColumn: "applyTime",
        isAsc: "desc"
      },
      url: {
        list: '/payApply/payApply/selectListByBuy',
        remove: '/payApply/payApply/',
        cancel: '/payApply/payApply/cancel/',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  methods: {
    /** 排序触发事件 */
		handleSortChange(column, prop, order) {
		  this.queryParams.orderByColumn = column.prop;
		  this.queryParams.isAsc = column.order;
		  this.handleQuery();
		},    /** 查询付款申请单列表 */
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAddMaterial() {
      this.$refs.buyListModule.show();
    },


    /** 新增按钮操作 */
    handleAddArrears() {
      this.$refs.buyListModuleDeposit.show("Y","3");
    },

    handleAddDeposit() {
      this.$refs.buyListModuleDeposit.show("Y","0");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.remove, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },


    handleAddHardware() {
      this.$refs.hardwareListForm.show("N","0");
    },

    payRecordForm(row) {
      this.$refs.payRecordForm.handleAddByApply(row);
    },

    handleDetail(row) {
      this.$refs.payApplyDetail.showDetail(row.id, row.buyId);
    },

    handlePrint(row) {
      this.$refs.payApplyDetailPrint.showDetail(row.id, row.buyId);
    },



    payRecordList(row) {
      let applyId = row.id;
      this.$refs.payRecordList.showPayRecordList(applyId);
    },

    showByBuy(buyId){
      this.title = "付款申请单记录";
      this.open =true;
      this.queryParams.buyId = buyId;
      this.getList();
    },

    getSummaries(param) {
      const {columns, data} = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        // 只合计amount1和amount3，想合并哪些列就在这里指定
        if ( column.property === "totalMoney" || column.property === "paidMoney" || column.property === "remainMoney") {
          sums[index] = values.reduce((prev, curr) => {

            const value = Number(curr);
            if (!isNaN(value)) {
              return accAdd(prev , curr);
            } else {
              return prev;
            }
          }, 0);
        }
      });
      return sums;
    },


  }
};
</script>
